package com.fancy.learncenter.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.fancy.learncenter.R;
import com.fancy.learncenter.bean.BaseBean;
import com.fancy.learncenter.bean.CartoonTeacherWorkListDataBean;
import com.fancy.learncenter.bean.StudentDataBean;
import com.fancy.learncenter.bean.TeacherStutasDataBean;
import com.fancy.learncenter.net.CustomNetCallBack;
import com.fancy.learncenter.net.CustomNetSubscriber;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.ui.activity.base.BaseActivity;
import com.fancy.learncenter.utils.NetUtil;
import com.fancy.learncenter.utils.ToastUtil;
import com.fancy.learncenter.utils.Utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.ResponseBody;
import retrofit2.Response;

public class CartoonAddTeacherActivity extends BaseActivity {

    public static String FLAG_KEY = "FLAG_KEY";
    public static String MOBILE_KEY = "MOBILE_KEY";
    public static String ROLE_KEY = "ROLE_KEY";
    public static String ROLE_DATA_KEY = "ROLE_DATA_KEY";
    @Bind(R.id.num_tv)
    TextView numTv;
    @Bind(R.id.simpleDraweeView)
    SimpleDraweeView simpleDraweeView;

    //1 老师 2学生
    private String roleStr = "0";

    ArrayList<String> itemDatas;
    @Bind(R.id.add_bt)
    Button addBt;
    @Bind(R.id.name)
    TextView name;
    @Bind(R.id.mobile_tv)
    TextView mobileTv;
    @Bind(R.id.description)
    TextView description;


    StudentDataBean studentDataBean;

    TeacherStutasDataBean.TeacherBean teacherBean;


    String mobile;
    String pageFlag;
    String classId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cartoon_add_teacher, Status.STATUS);
        ButterKnife.bind(this);

        classId = getIntent().getStringExtra(CartoonTeacherClassDetailActivity.CLASS_ID_KEY);
        roleStr = getIntent().getStringExtra(ROLE_KEY);
        nameStr = getIntent().getStringExtra("nameStr");


        if ("2".equals(roleStr)) {
            initToolbar("学生信息");

            studentDataBean = (StudentDataBean) getIntent().getSerializableExtra(ROLE_DATA_KEY);

            pageFlag = studentDataBean.getStudent().getPageFlag();

            mobile = studentDataBean.getStudent().getMobile();

            if (pageFlag.equals("1")) {
                name.setText("未注册");

                name.setText("学生");
                mobileTv.setText(mobile);
                description.setVisibility(View.VISIBLE);
                description.setText("邀请学生，学生会收到邀请短信，自主输入班级号加入班级");

            } else if (pageFlag.equals("2")) {

                name.setText(studentDataBean.getStudent().getName());

                name.setText("学生");

                mobileTv.setText(mobile);

                numTv.setVisibility(View.VISIBLE);

                description.setVisibility(View.VISIBLE);

                description.setText("邀请学生，该学生激活账号后自动加入班级");

                simpleDraweeView.setImageURI(studentDataBean.getStudent().getImageUrl());

            } else if (pageFlag.equals("3")) {

                if (TextUtils.isEmpty(studentDataBean.getStudent().getName())) {
                    name.setText("学生");
                } else {
                    name.setText(studentDataBean.getStudent().getName());
                }


                mobileTv.setText(mobile);

                numTv.setVisibility(View.VISIBLE);
                description.setVisibility(View.GONE);

                simpleDraweeView.setImageURI(studentDataBean.getStudent().getImageUrl());
            } else if (pageFlag.equals("4")) {

                if (TextUtils.isEmpty(studentDataBean.getStudent().getName())) {
                    name.setText("学生");
                } else {
                    name.setText(studentDataBean.getStudent().getName());
                }


                mobileTv.setText(mobile);
                description.setText("该学生已在班中");
                description.setVisibility(View.VISIBLE);
                addBt.setText("确定");
                simpleDraweeView.setImageURI(studentDataBean.getStudent().getImageUrl());
                simpleDraweeView.setVisibility(View.VISIBLE);
            }

        } else {
            initToolbar("老师信息");

            teacherBean = (TeacherStutasDataBean.TeacherBean) getIntent().getSerializableExtra(ROLE_DATA_KEY);

            pageFlag = teacherBean.getPageFlag();

            mobile = teacherBean.getMobile();

            if (pageFlag.equals("1")) {
                name.setText("未注册");

                mobileTv.setText(teacherBean.getMobile());

                description.setText("邀请老师，该老师激活账号后将自动加入班级");

            } else if (pageFlag.equals("2")) {

//                name.setText(teacherBean.getPageFlag());

                name.setText("老师");

                mobileTv.setText(teacherBean.getMobile());

                simpleDraweeView.setImageURI(teacherBean.getImageUrl());
                simpleDraweeView.setVisibility(View.VISIBLE);

                description.setVisibility(View.VISIBLE);

                description.setText("邀请老师，该老师激活账号后自动加入班级");


            } else if (pageFlag.equals("3")) {

//                name.setText(teacherBean.getPageFlag());

                name.setText("老师");

                mobileTv.setText(teacherBean.getMobile());

                simpleDraweeView.setImageURI(teacherBean.getImageUrl());
                simpleDraweeView.setVisibility(View.VISIBLE);
            } else if (pageFlag.equals("4")) {

                name.setText("老师");

                mobileTv.setText(teacherBean.getMobile());

                description.setText("该老师已在班中");
                description.setVisibility(View.VISIBLE);

                addBt.setText("确定");

                simpleDraweeView.setImageURI(teacherBean.getImageUrl());
                simpleDraweeView.setVisibility(View.VISIBLE);
            }
        }


    }

    boolean isSub = false;

    @OnClick(R.id.add_bt)
    public void onViewClicked() {


        if (!pageFlag.equals("4")) {

            if (!isSub) {
                isSub = true;
                if ("2".equals(roleStr)) {
                    addStudent();
                } else {
                    addTeacher();
                }
            }


        } else {
            finish();
        }

    }

    String nameStr = "";

    private void addTeacher() {
        Map<String, Object> map = new HashMap<>();
        map.put("classId", classId);
        map.put("mobile", mobile);
        if (!TextUtils.isEmpty(nameStr)) {
            map.put("name", nameStr);
        }
        map.put("pageFlag", pageFlag);

        HttpMehtod.getInstance().addTeacher(map, new CustomNetSubscriber(this, true) {
            @Override
            public void onNext(Response<ResponseBody> responseBodyResponse) {
                isSub = false;

                BaseBean<CartoonTeacherWorkListDataBean> baseBean = Utils.fromJsonObject(NetUtil.getResResult(responseBodyResponse), CartoonTeacherWorkListDataBean.class);
                CustomNetCallBack callBack = new CustomNetCallBack(getContext(), baseBean);
                callBack.setCallBack(new CustomNetCallBack.NetCallBack<CartoonTeacherWorkListDataBean>() {
                    @Override
                    public void success(CartoonTeacherWorkListDataBean dataBean) {
                        ToastUtil.show("邀请成功");
                        finish();
                    }

                    @Override
                    public void fail(String errorCode) {


                    }
                }).dealData();
            }

        });
    }

    private void addStudent() {
        Map<String, Object> map = new HashMap<>();
        map.put("classId", classId);
        map.put("mobile", mobile);
        map.put("pageFlag", pageFlag);
        if (!TextUtils.isEmpty(nameStr)) {
            map.put("name", nameStr);
        }

        HttpMehtod.getInstance().addStudent(map, new CustomNetSubscriber(this, true) {
            @Override
            public void onNext(Response<ResponseBody> responseBodyResponse) {
                isSub = false;

                BaseBean<CartoonTeacherWorkListDataBean> baseBean = Utils.fromJsonObject(NetUtil.getResResult(responseBodyResponse), CartoonTeacherWorkListDataBean.class);
                CustomNetCallBack callBack = new CustomNetCallBack(getContext(), baseBean);
                callBack.setCallBack(new CustomNetCallBack.NetCallBack<CartoonTeacherWorkListDataBean>() {
                    @Override
                    public void success(CartoonTeacherWorkListDataBean dataBean) {
                        ToastUtil.show("邀请成功");
                        finish();
                    }

                    @Override
                    public void fail(String errorCode) {


                    }
                }).dealData();
            }

        });
    }
}
